package com.frank.loadinglibrary.base.basecomponent;

import com.frank.loadinglibrary.base.baseelement.BaseElement;
import com.frank.loadinglibrary.base.baseelement.IComponentCallback;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.app.Context;

/**
 * @author eric_shaw
 * @version 1.0
 * @description 组件基类
 * @classname BaseComponent.class
 * @program loadinglibrary
 * @date 2021/06/07
 **/
public class BaseComponent<T extends BaseElement> extends Component implements Component.DrawTask, Component.BindStateChangedListener, IComponentCallback {

    /**
     * 配置动画是否自动开启
     */
    protected boolean autoStart = true;

    protected T element;

    public BaseComponent(Context context) {
        this(context, null);
    }

    public BaseComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }


    public BaseComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        init(context);
    }

    public BaseComponent(Context context, AttrSet attrSet, int resId) {
        super(context, attrSet, resId);
        init(context);
    }

    /**
     * 初始化
     * @param ctx
     */
    public void init(Context ctx) {
        setBindStateChangedListener(this);
        addDrawTask(this);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (element != null) {
            element.onMeasure(getWidth(), getHeight());
            element.draw(canvas);
        }
    }


    @Override
    public void onComponentBoundToWindow(Component component) {
        if (element != null) {
            element.initPaint();
        }
        if (autoStart && element != null) {
            element.startAnimators();
        }
    }

    @Override
    public void onComponentUnboundFromWindow(Component component) {
        //从窗口移除
        if (autoStart && element != null) {
            element.stopAnimators();
        }
    }

    public Component setAutoAnimation(boolean auto) {
        this.autoStart = auto;
        return this;
    }

    public void startAnimators() {
        if (element != null) {
            element.startAnimators();
        }
    }

    public void stopAnimators() {
        if (element != null) {
            element.stopAnimators();
        }
    }

    public void pauseAnimation() {
        if (element != null) {
            element.pauseAnimation();
        }
    }

    public void resumeAnimation() {
        if (element != null) {
            element.resumeAnimation();
        }
    }

    public void resetAnimation() {
        if (element != null) {
            element.resetAnimation();
        }
    }

    public void releaseSource() {
        if (element != null) {
            element.release();
        }
    }


    @Override
    public void setVisibility(int v) {
        if (getVisibility() != v) {
            super.setVisibility(v);
            if (v != VISIBLE) {
                startAnimators();
            } else {
                if (autoStart) {
                    stopAnimators();
                }
            }
        }
    }

    public void config(T e) {
        this.element = e;
        if (element != null) {
            element.initPaint();
        }
    }

    protected void show() {
        setVisibility(VISIBLE);
    }

    protected void hide() {
        setVisibility(HIDE);
    }

    @Override
    public void reflushComponent() {
        invalidate();
    }

    @Override
    public AnimatorProperty getAnimatorProperty() {
        return createAnimatorProperty();
    }

    @Override
    public Context getContext() {
        return super.getContext();
    }
}
